<template>
  <div class="article-timeline">
    <h2 class="timeline-title">文章时间轴</h2>
    <div class="timeline-container">
      <div 
        class="timeline-item" 
        v-for="article in articles" 
        :key="article.id"
        @click="viewArticle(article.id)"
      >
        <div class="timeline-dot"></div>
        <div class="timeline-date">{{ formatDate(article.createTime) }}</div>
        <div class="timeline-content">
          <h3>{{ article.title }}</h3>
          <p>{{ article.summary }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getArticleList } from '@/api/article';

const router = useRouter();
const articles = ref([]);

onMounted(async () => {
  try {
    const res = await getArticleList({ pageNum: 1, pageSize: 10 });
    articles.value = res.records;
  } catch (error) {
    console.error('获取文章列表失败', error);
  }
});

const formatDate = (dateStr) => {
  if (!dateStr) return '';
  const date = new Date(dateStr);
  return date.toLocaleDateString('zh-CN', { 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric' 
  });
};

const viewArticle = (id) => {
  router.push(`/article/${id}`);
};
</script>

<style scoped>
.article-timeline {
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.timeline-title {
  text-align: center;
  margin-bottom: 25px;
  color: #3a3a3a;
  font-size: 1.5rem;
}

.timeline-container {
  position: relative;
  padding-left: 30px;
}

.timeline-container:before {
  content: '';
  position: absolute;
  left: 10px;
  top: 0;
  height: 100%;
  width: 2px;
  background: linear-gradient(to bottom, #42b983, #64b5f6);
}

.timeline-item {
  position: relative;
  margin-bottom: 30px;
  cursor: pointer;
  transition: transform 0.3s;
}

.timeline-item:hover {
  transform: translateX(5px);
}

.timeline-dot {
  position: absolute;
  left: -30px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: linear-gradient(135deg, #42b983, #64b5f6);
  top: 5px;
  box-shadow: 0 0 0 4px rgba(100, 181, 246, 0.2);
}

.timeline-date {
  font-size: 0.85rem;
  color: #666;
  margin-bottom: 5px;
}

.timeline-content {
  background-color: white;
  padding: 15px;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.timeline-content h3 {
  margin-top: 0;
  color: #42b983;
}

.timeline-content p {
  color: #666;
  margin-bottom: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style> 